****This code replicates the figure and models in: Blais, Andr�, Shane P. Singh, and Delia Dumitrescu. 2014. "Political Institutions, Perceptions of Representation, and the Turnout Decision." In Elections and Democracy: Representation and Accountability, ed., J. Thomassen. Oxford: Oxford University Press, 99-112.



***get sample with reference to data availability on macro-level variables
**without BELG because missing data on representation variable, and without NLD because missing the efficacy variable
reg proporcomp freedom party_age exeelec polarize enep if legelec == 1 & cntryyear ~= "NLD_2002" & cntryyear ~= "BEL_2003"
gen samp =  1 if e(sample)


***apply the survey weights
gen weight = 1 if voted ~=. & samp  == 1
gen repturnout = .

sum voted if cntryyear == "ALB_2005" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "ALB_2005"
replace weight = actturnout/repturnout if cntryyear == "ALB_2005" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "ALB_2005" & samp == 1 & voted == 0

sum voted if cntryyear == "AUS_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "AUS_2004"
replace weight = actturnout/repturnout if cntryyear == "AUS_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "AUS_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "BEL_2003" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "BEL_2003"
replace weight = actturnout/repturnout if cntryyear == "BEL_2003" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "BEL_2003" & samp == 1 & voted == 0

sum voted if cntryyear == "BGR_2001" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "BGR_2001"
replace weight = actturnout/repturnout if cntryyear == "BGR_2001" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "BGR_2001" & samp == 1 & voted == 0

sum voted if cntryyear == "BRA_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "BRA_2002"
replace weight = actturnout/repturnout if cntryyear == "BRA_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "BRA_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "CAN_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "CAN_2004"
replace weight = actturnout/repturnout if cntryyear == "CAN_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "CAN_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "CHE_2003" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "CHE_2003"
replace weight = actturnout/repturnout if cntryyear == "CHE_2003" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "CHE_2003" & samp == 1 & voted == 0

sum voted if cntryyear == "CHL_2005" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "CHL_2005"
replace weight = actturnout/repturnout if cntryyear == "CHL_2005" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "CHL_2005" & samp == 1 & voted == 0

sum voted if cntryyear == "CZE_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "CZE_2002"
replace weight = actturnout/repturnout if cntryyear == "CZE_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "CZE_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "DEU12002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "DEU12002"
replace weight = actturnout/repturnout if cntryyear == "DEU12002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "DEU12002" & samp == 1 & voted == 0

sum voted if cntryyear == "DNK_2001" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "DNK_2001"
replace weight = actturnout/repturnout if cntryyear == "DNK_2001" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "DNK_2001" & samp == 1 & voted == 0

sum voted if cntryyear == "ESP_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "ESP_2004"
replace weight = actturnout/repturnout if cntryyear == "ESP_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "ESP_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "FIN_2003" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "FIN_2003"
replace weight = actturnout/repturnout if cntryyear == "FIN_2003" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "FIN_2003" & samp == 1 & voted == 0

sum voted if cntryyear == "FRA_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "FRA_2002"
replace weight = actturnout/repturnout if cntryyear == "FRA_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "FRA_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "GBR_2005" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "GBR_2005"
replace weight = actturnout/repturnout if cntryyear == "GBR_2005" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "GBR_2005" & samp == 1 & voted == 0

sum voted if cntryyear == "HUN_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "HUN_2002"
replace weight = actturnout/repturnout if cntryyear == "HUN_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "HUN_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "IRL_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "IRL_2002"
replace weight = actturnout/repturnout if cntryyear == "IRL_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "IRL_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "ISL_2003" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "ISL_2003"
replace weight = actturnout/repturnout if cntryyear == "ISL_2003" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "ISL_2003" & samp == 1 & voted == 0


sum voted if cntryyear == "ISR_2003" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "ISR_2003"
replace weight = actturnout/repturnout if cntryyear == "ISR_2003" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "ISR_2003" & samp == 1 & voted == 0


sum voted if cntryyear == "ITA_2006" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "ITA_2006"
replace weight = actturnout/repturnout if cntryyear == "ITA_2006" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "ITA_2006" & samp == 1 & voted == 0

sum voted if cntryyear == "JPN_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "JPN_2004"
replace weight = actturnout/repturnout if cntryyear == "JPN_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "JPN_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "KOR_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "KOR_2004"
replace weight = actturnout/repturnout if cntryyear == "KOR_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "KOR_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "MEX_2003" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "MEX_2003"
replace weight = actturnout/repturnout if cntryyear == "MEX_2003" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "MEX_2003" & samp == 1 & voted == 0

sum voted if cntryyear == "NLD_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "NLD_2002"
replace weight = actturnout/repturnout if cntryyear == "NLD_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "NLD_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "NOR_2001" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "NOR_2001"
replace weight = actturnout/repturnout if cntryyear == "NOR_2001" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "NOR_2001" & samp == 1 & voted == 0

sum voted if cntryyear == "NZL_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "NZL_2002"
replace weight = actturnout/repturnout if cntryyear == "NZL_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "NZL_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "PER_2006" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "PER_2006"
replace weight = actturnout/repturnout if cntryyear == "PER_2006" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "PER_2006" & samp == 1 & voted == 0

sum voted if cntryyear == "PHL_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "PHL_2004"
replace weight = actturnout/repturnout if cntryyear == "PHL_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "PHL_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "POL_2001" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "POL_2001"
replace weight = actturnout/repturnout if cntryyear == "POL_2001" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "POL_2001" & samp == 1 & voted == 0

sum voted if cntryyear == "PRT_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "PRT_2002"
replace weight = actturnout/repturnout if cntryyear == "PRT_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "PRT_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "PRT_2005" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "PRT_2005"
replace weight = actturnout/repturnout if cntryyear == "PRT_2005" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "PRT_2005" & samp == 1 & voted == 0

sum voted if cntryyear == "ROU_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "ROU_2004"
replace weight = actturnout/repturnout if cntryyear == "ROU_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "ROU_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "RUS_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "RUS_2004"
replace weight = actturnout/repturnout if cntryyear == "RUS_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "RUS_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "SVN_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "SVN_2004"
replace weight = actturnout/repturnout if cntryyear == "SVN_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "SVN_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "SWE_2002" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "SWE_2002"
replace weight = actturnout/repturnout if cntryyear == "SWE_2002" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "SWE_2002" & samp == 1 & voted == 0

sum voted if cntryyear == "TWN_2001" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "TWN_2001"
replace weight = actturnout/repturnout if cntryyear == "TWN_2001" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "TWN_2001" & samp == 1 & voted == 0

sum voted if cntryyear == "TWN_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "TWN_2004"
replace weight = actturnout/repturnout if cntryyear == "TWN_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "TWN_2004" & samp == 1 & voted == 0

sum voted if cntryyear == "USA_2004" & samp == 1
replace repturnout = r(mean)*100 if cntryyear == "USA_2004"
replace weight = actturnout/repturnout if cntryyear == "USA_2004" & samp == 1 & voted == 1
replace weight = (100 - actturnout)/(100 - repturnout) if cntryyear == "USA_2004" & samp == 1 & voted == 0


svyset _n [pweight=weight], strata(cntryyearnum)

*test to see if weights work properly
mean voted if samp == 1 & cntryyear == "ALB_2005"
svy: mean voted if samp == 1 &  cntryyear == "ALB_2005"
mean actturnout if cntryyear == "ALB_2005"

mean voted if samp == 1 & cntryyear == "USA_2004"
svy: mean voted if samp == 1 &  cntryyear == "USA_2004"
mean actturnout if cntryyear == "USA_2004"



***Figure 6.1

preserve
collapse actturnout samp,  by(countryandyear)
#delimit;
graph dot (asis) actturnout if samp == 1, over(countryandyear, sort(actturnout) label(labsize(small)))  exclude0  
linetype(line) lines(lpattern(dash))
marker(1, msymbol(circle) msize(medsmall
)  mcolor(black)) ysize(13) xsize(8) scheme(s1color)   ytitle(Turnout (%))
;
restore


***Table 6.1
svy: tabulate voted represented if samp == 1, column 

*Model A
svy: logit voted represented  age female income educ  id efficacycont if samp == 1

*Model B
preserve
collapse actturnout voted nexepar_97p nfeduni_97p ncdd_97p nexepar_97p_twodim nfeduni_97p_twodim nexepar_97p_twodi_fixed nfeduni_97p_twodim_fixed represented id disprop proporcomp compsanc gdppercapPPP switz freedom party_age polarize enep exeelec executive legelec samp , by(countryandyear)
reg represented nexepar_97p_twodi_fixed nfeduni_97p_twodim_fixed party_age  if samp == 1
restore

*Model C
preserve
collapse actturnout voted nexepar_97p nfeduni_97p ncdd_97p nexepar_97p_twodim nfeduni_97p_twodim nexepar_97p_twodi_fixed nfeduni_97p_twodim_fixed represented id disprop proporcomp compsanc gdppercapPPP switz freedom party_age polarize enep exeelec executive legelec samp , by(countryandyear)
reg represented  proporcomp  party_age if samp == 1
restore

*Model D
preserve
collapse actturnout voted nexepar_97p nfeduni_97p ncdd_97p nexepar_97p_twodim nfeduni_97p_twodim nexepar_97p_twodi_fixed nfeduni_97p_twodim_fixed represented id disprop proporcomp compsanc gdppercapPPP switz freedom party_age polarize enep exeelec executive legelec samp , by(countryandyear)
reg represented  proporcomp party_age polarize enep if  samp == 1
restore

*Model E
gllamm voted  age female income educ   proporcomp compsanc if samp == 1 & represented~=. & efficacycont~=. & id~=., i(cntryyearnum) link(logit) family(binomial) nip(4) pweight(weight) 

*Model F
gllamm voted  age female income educ    nexepar_97p_twodi_fixed nfeduni_97p_twodim_fixed compsanc if samp == 1 & represented~=. & efficacycont~=. & id~=., i(cntryyearnum) link(logit) family(binomial) nip(4) pweight(weight) 

*Model G
gllamm voted represented  age female income educ  id efficacycont  proporcomp compsanc if samp == 1, i(cntryyearnum) link(logit) family(binomial) nip(4) pweight(weight) 

*Model H
gen repXporc = represented*proporcomp
gen repXcmpls = represented*compsanc
gllamm voted represented  age female income educ  id efficacycont  proporcomp compsanc repXporc repXcmpls if samp == 1, i(cntryyearnum) link(logit) family(binomial) nip(4) pweight(weight) 


